class_name ICharacter extends CharacterBody2D

const stateList:Array[String]= ["idle","walk","chase","hit","hurt","death"]
const orientationList:Array[String]= ["up","down","left","right"]

func _ready() -> void:
	# test data
	buildAnimeSprite({
		"path":"res://art/characters/player.png",
		"actionMap":{
			"idle_down":[{"x":0,"y":0,"w":48,"h":48},
				{"x":48,"y":0,"w":48,"h":48},{"x":96,"y":0,"w":48,"h":48}],
		}
	})
	$AnimatedSprite2D.play("idle_down")
	position=Vector2i(96,48)
	# test end
	pass
func _process(_delta: float) -> void:
	pass

func initialize(config:Dictionary)->void:
	# 构建行走图
	buildAnimeSprite(config.get("actionImg"))
	# 构建碰撞体
	# 初始化属性
	pass
func inputListener(_delta:float)->void:
	if Input.is_action_pressed("move_down"):
		pass
	pass
func eventListener(_delta:float)->void:pass
func updatePosition(_x:int,_y:int)->void:pass
func updateState(state:String)->void:
	var actionMap:Dictionary={}
	for key in actionMap:
		if state==key:
			var fun:Callable= actionMap[key]
			fun.call()
			break
	pass

func buildAnimeSprite(config:Dictionary)->void:
	var path:String= config.get("path")
	#var actionImg:Image= Image.load_from_file(path)
	#var texture:ImageTexture = ImageTexture.create_from_image(actionImg)
	var texture:CompressedTexture2D= load(path)
	var actionMap:Dictionary= config.get("actionMap")
	var spriteFrames:SpriteFrames= $AnimatedSprite2D.sprite_frames
	for actionName:String in actionMap:
		var actionArray:Array= actionMap[actionName]
		spriteFrames.add_animation(actionName)
		for frame:Dictionary in actionArray:
			var x:int= frame.get("x")
			var y:int= frame.get("y")
			var w:int= frame.get("w")
			var h:int= frame.get("h")
			var img:ImageTexture= TextureUtils.get_region(texture, Rect2i(x,y,w,h))
			spriteFrames.add_frame(actionName,img,1,-1)
	pass
